<template>
    <div class="qingwu">
        <div class="admin_table_page_title">
            <a-button @click="$router.back()" class="float_right" icon="arrow-left">返回</a-button>
            小区编辑
        </div>
        <div class="unline underm"></div>
        <div class="admin_form">
            <a-form-model :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
                <a-form-model-item label="小区名称">
                    <a-input v-model="info.community_name" placeholder="请输入小区名称"></a-input>
                </a-form-model-item>
                <a-form-model-item label="省/市/区">
                    <a-cascader :options="options" placeholder="请选择地址" :field-names="{ label: 'name', value: 'id', children: 'children' }" :default-value="areas" @change="onChange" />
                </a-form-model-item>
                <a-form-model-item label="小区地址">
                    <a-input v-model="info.community_address" placeholder="请输入小区地址"></a-input>
                </a-form-model-item>

                <a-form-item label="小区物业">
                    <a-select  @change="handleChange" :default-value="default_property_id" >
                        <a-select-option  :value="value.id" v-for="(value,index) in property_lists" :key="index">
                            {{ value.name }}
                        </a-select-option>
                    </a-select>
                </a-form-item>

                <a-form-model-item :wrapper-col="{ span: 12, offset: 5 }">
                    <a-button type="primary" @click="handleSubmit">提交</a-button>
                </a-form-model-item>
            </a-form-model>

        </div>
    </div>
</template>

<script>
export default {
    components: {},
    props: {},
    data() {
        return {
            info:{
            },
            id:0,
            options: [],
            areas:[],
            property_lists:[],
            params:{
                page:1,
                per_page:3000,
                check: 1
            },
            default_property_id:'请选择物业',
            property_id:''
        };
    },
    watch: {},
    computed: {},
    methods: {
        handleSubmit(){

            // 验证代码处
            if(this.$isEmpty(this.info.community_name)){
                return this.$message.error('小区名称不能为空');
            }
            if(this.$isEmpty(this.info.community_address)){
                return this.$message.error('小区地址不能为空');
            }
            if( this.areas.length == 0 ){
                return this.$message.error('请选择省市区');
            }

            let api = this.$apiHandle(this.$api.adminCommunity,this.id);

            this.info.areas = this.areas;
            this.info.property_id = this.property_id;

            if(api.status){
                this.$put(api.url,this.info).then(res=>{
                    if(res.code == 200){
                        this.$message.success(res.msg)
                        return this.$router.back();
                    }else{
                        return this.$message.error(res.msg)
                    }
                })
            }else{
                this.$post(api.url,this.info).then(res=>{
                    if(res.code == 200){
                        this.$message.success(res.msg)
                        return this.$router.back();
                    }else{
                        return this.$message.error(res.msg)
                    }
                })
            }
        },
        get_info(){
            this.$get(this.$api.adminCommunity+'/'+this.id).then(res=>{
                this.info = res.data;
                this.areas[0] = res.data.province;
                this.areas[1] = res.data.city;
                this.areas[2] = res.data.area;
            })
        },
        // 获取城市列表
        get_area(){
            this.$get(this.$api.areasLists).then(res=>{
                this.options = res.data;
            })
        },
        // 省市区发生改变事件
        onChange(value, selectedOptions){
            this.areas = value;
        },
        // 获取列表
        onload(){
            // 判断你是否是编辑
            if(!this.$isEmpty(this.$route.params.id)){
                this.id = this.$route.params.id;
                this.get_info();
            }
        },
        // 获取物业列表
        getPropertyLists(){
            this.$get(this.$api.adminProperty,this.params).then(res=>{
                this.property_lists = res.data.data;
            })
        },
        // 小区选择
        handleChange(value){
            this.property_id = value;
        }
    },
    created() {
        this.get_area();
        this.onload();
        this.getPropertyLists();
    },
    mounted() {}
};
</script>
<style lang="scss" scoped>

</style>
